//
//  SettingTableViewController.m
//  IMHX_Demo
//
//  Created by 郭健 on 16/3/31.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "SettingTableViewController.h"
#import "LoginViewController.h"
#import "CurrentUserInfo.h"

#define kColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.4]
@interface SettingTableViewController ()

@property (nonatomic,retain)NSMutableArray *context;

@end

@implementation SettingTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = kColor;
//    self.view.backgroundColor = kColor;
    self.navigationItem.title = @"设置";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"退出登录" style:UIBarButtonItemStylePlain target:self action:@selector(logout)];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

-(NSMutableArray *)context{
    
    if (!_context) {
        _context = [[NSMutableArray alloc]initWithObjects:@"自动登录",@"消息推送设置",@"黑名单",@"诊断",@"退群时删除回话",@"iOS离线推送昵称",@"个人信息", nil];
    }
    return _context;
}

//退出登录
-(void)logout{

    //从服务器退出
    EMError *error = [[EMClient sharedClient] logout:YES];
    [AVUser logOut];  //清除缓存用户对象
    //本地用户信息清空
    if (error) {
        NSLog(@"退出失败  -- %d",error.code);
        return;
    }else{
        CurrentUserInfo *currentUserInfo = [CurrentUserInfo sharedCurrentUserInfo];
        currentUserInfo.userName = @"";
        currentUserInfo.pwd = @"";
        currentUserInfo.isLogin = NO;
        
            //退回到登录界面
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:loginVC];
            loginVC.state = fromMainViewControllerState;
            
            //将TabBar 的子控制器切换到联系人界面
            self.navigationController.tabBarController.selectedIndex = 0;
            [self.navigationController.tabBarController presentViewController:navC animated:YES completion:nil];

        
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.context.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [self.context objectAtIndex:indexPath.row];   
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
